package cn.com.demo.microservice.consumer.service.impl;

import cn.com.demo.microservice.consumer.service.ISecurityService;
import cn.com.demo.microservice.consumer.service.feign.IAccountSearchFeginService;
import cn.com.demo.microservice.entity.AqAccount;
import cn.com.demo.microservice.utils.JwtUtil;
import cn.com.demo.microservice.vo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SecurityServiceImpl implements ISecurityService {
    @Autowired
    private IAccountSearchFeginService accountFeginService;
    @Override
    public String verifyAccount(Account account) {
        String token = null;
        // 调用提供者服务，获取账号对应的对象-数据库中的
        AqAccount dbAccount = this.accountFeginService.searchAccountByCode(account.getUsername());
        // 比对密码
        if(dbAccount.getAccPassword().equals(account.getPassword())) {
            //   ok，生成token返回
            token = JwtUtil.sign(account.getUsername(), JwtUtil.CLAIM_LOGIN_NAME);
        }else {
            //   否则：抛异常
            throw new RuntimeException("密码不匹配");
        }
        return token;
    }
}
